<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Login_lib{
	var $file = null;
	var $id_field = 'id';
	var $role = '';
	var $extra_fields = null;
	
	function Login_lib($params = null){
		$this->CI = &get_instance();
		$this->CI->load->library('session');
		$this->CI->load->helper('security');
		if( is_array($params) && isset($params['file']) && file_exists($params['file'])){
			$this->file = $params['file'];
		}
		$this->table = isset($params['table'])?$params['table']:null;
		$this->id_field = isset($params['id_field'])?$params['id']:$this->id_field;
		$this->encrypt = isset($params['encrypt']) && !empty($params['encrypt'])?$params['encrypt']:'md5';
		$this->role = isset($params['role'])?$params['role']:$this->role;
		$this->extra_fields = isset($params['extra_fields'])?$params['extra_fields']:array();
	}
	
	function setFile( $file ){
		if( file_exists($file)){
			$this->file = $file;
			//echo $this->file . '<br />';
		}
	}
	
	function doLogin( $username, $password){
		$result = false;
		if( $this->table ){
			$result = $this->_checkLoginDB( $username, $password);
		}else{
			$result = $this->_checkLoginFile( $username, $password);
		}
		if( $result !== false ){
			$this->setLoggedIn($username, $result);
			return true;
		}
		return false;
	}
	
	function _checkLoginFile( $username, $password){
		if( !empty($this->file)){
			$file = @fopen($this->file, "r");
			if( $file ){
				$password = dohash($password, $this->encrypt);
				while( !feof($file) ){
					$line = trim(fgets($file));
					if( !empty($line) ){
						list($u, $p) = split(':', $line);
						if( $u === $username && $p === $password){
							return true;
						}
					}
				}
				return false;
			}else{
				show_error('Login_lib: Could not open password file!');
			}
			return false;
		}else{
			show_error('Login_lib: No password file was specified!');
			return false;
		}
	}
	
	function _checkLoginDB( $username, $password){
		$this->CI->load->database();
		if( !empty($this->table) && $this->CI->db->table_exists($this->table)){
			$password = dohash($password, $this->encrypt);
			//echo "user: $username, password: $password<br />";
			$check = array_merge($this->extra_fields, array('username' => $username, 'password' => $password));
			$this->CI->db->where( $check );
			$r = $this->CI->db->get($this->table, 1);
			if( $r && $r->num_rows() === 1){
				$r = $r->first_row('array');
				if( isset($r[$this->id_field]) ){
					return $r[$this->id_field];
				}
				return true;
			}
			return false;
		}else{
			show_error('Login_lib: Table does not exist');
			return false;
		}
	}
	
	function setLoggedIn( $username, $id = null ){
		$this->CI->session->set_userdata(
			array(
				'logged_in' => $this->role,
				'username'	=> $username,
				'userid'	=> $id
			)
		);
	}
	
	function logout( ){
		$this->CI->session->unset_userdata(
			array(
				'logged_in'	=> '',
				'username'	=> '',
				'userid' => ''
			)
		);
	}
	
	function isLoggedIn($role){
		return ($this->CI->session->userdata('logged_in')==$role);
	}
}
